package com.ilxqx.generator.parser.impl;

import com.ilxqx.generator.parser.Rules;
import com.ilxqx.generator.po.Rule;

import java.util.List;
import java.util.Optional;

/**
 * 规则实现类
 * @author venus
 */
public class RulesImpl implements Rules {

    private List<Rule> rules;

    public RulesImpl(List<Rule> rules) {
        this.rules = rules;
    }

    /**
     * 是否匹配ruleName所指定的规则
     *
     * @param ruleName 规则名称
     */
    @Override
    public boolean match(String ruleName) {
        return this.rules.stream().anyMatch(rule -> rule.getRuleName().equals(ruleName));
    }

    /**
     * 获取规则
     *
     * @param ruleName 规则名称
     * @return 规则对象
     */
    @Override
    public Rule getRule(String ruleName) {
        Optional<Rule> ruleOptional = this.rules.stream().filter(rule -> rule.getRuleName().equals(ruleName)).findFirst();
        return ruleOptional.orElse(null);
    }

}
